package com.anti.modular.bus.busmeansoftransaction.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.buscostincomeexpenses.param.BusCostIncomeExpensesParam;
import com.anti.modular.bus.busmeansoftransaction.entity.BusMeansOfTransaction;
import com.anti.modular.bus.busmeansoftransaction.enums.BusMeansOfTransactionExceptionEnum;
import com.anti.modular.bus.busmeansoftransaction.param.BusMeansOfTransactionParam;
import com.anti.modular.bus.busmeansoftransaction.service.BusMeansOfTransactionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 交易方式service接口实现类
 *
 * @author anti
 * @date 2021-04-08 19:35:07
 */
@Service
public class BusMeansOfTransactionServiceImpl implements BusMeansOfTransactionService {
    @Resource
    DBUtils dbUtils;

    @Override
    public BusMeansOfTransaction getById(BusMeansOfTransactionParam busMeansOfTransactionParam){
        String sql="select * from bus_means_of_transaction where id =? ";
        return dbUtils.get(sql,BusMeansOfTransaction.class,busMeansOfTransactionParam.getId());
    }

    @Override
    public BusMeansOfTransaction getOneById(Integer id) {
        String sql="select * from bus_means_of_transaction where id =? ";
        return dbUtils.getFirst(sql,BusMeansOfTransaction.class,id);
    }

    @Override
    public PageResult page(PageParam pageParam,BusMeansOfTransactionParam busMeansOfTransactionParam) {
        StringBuilder sb=new StringBuilder("select * from bus_means_of_transaction where status=0 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(busMeansOfTransactionParam)) {
            // 根据名称 查询
            if (ObjectUtil.isNotEmpty(busMeansOfTransactionParam.getName())) {
                sb.append(" and name like ?");
                param.add("%"+busMeansOfTransactionParam.getName()+"%");
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BusMeansOfTransaction> list(BusMeansOfTransactionParam busMeansOfTransactionParam) {
        String sql="select * from bus_means_of_transaction where status=0";
        return dbUtils.query(sql,BusMeansOfTransaction.class);
    }

    @Override
    public void add(BusMeansOfTransactionParam busMeansOfTransactionParam) {
        checkParam(busMeansOfTransactionParam,false);
        BusMeansOfTransaction busMeansOfTransaction = new BusMeansOfTransaction();
        BeanUtil.copyProperties(busMeansOfTransactionParam, busMeansOfTransaction);
        busMeansOfTransaction.setStatus(0);
        busMeansOfTransaction.setCreateTime(new Date());
        busMeansOfTransaction.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(busMeansOfTransaction);
    }


    public void delete(BusMeansOfTransactionParam busMeansOfTransactionParam) {
        BusMeansOfTransaction busMeansOfTransaction = this.queryBusMeansOfTransaction(busMeansOfTransactionParam);
        busMeansOfTransaction.setStatus(2);
        busMeansOfTransaction.setUpdateTime(new Date());
        busMeansOfTransaction.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busMeansOfTransaction);
    }

    public void edit(BusMeansOfTransactionParam busMeansOfTransactionParam) {
        checkParam(busMeansOfTransactionParam,true);
        BusMeansOfTransaction busMeansOfTransaction = this.queryBusMeansOfTransaction(busMeansOfTransactionParam);
        BeanUtil.copyProperties(busMeansOfTransactionParam, busMeansOfTransaction);
        busMeansOfTransaction.setUpdateTime(new Date());
        busMeansOfTransaction.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(busMeansOfTransaction);
    }

    @Override
    public BusMeansOfTransaction detail(BusMeansOfTransactionParam busMeansOfTransactionParam) {
        return this.queryBusMeansOfTransaction(busMeansOfTransactionParam);
    }

    /**
     * 获取交易方式
     *
     * @author anti
     * @date 2021-04-08 19:35:07
     */
    private BusMeansOfTransaction queryBusMeansOfTransaction(BusMeansOfTransactionParam busMeansOfTransactionParam) {
        BusMeansOfTransaction busMeansOfTransaction = this.getById(busMeansOfTransactionParam);
        if (ObjectUtil.isNull(busMeansOfTransaction)) {
            throw new BusinessException(BusMeansOfTransactionExceptionEnum.NOT_EXIST);
        }
        return busMeansOfTransaction;
    }
    private void checkParam(BusMeansOfTransactionParam busMeansOfTransactionParam, boolean isExcludeSelf){
        Long id = busMeansOfTransactionParam.getId();
        String depositName = busMeansOfTransactionParam.getName();

        //构建带name查询条件
        StringBuilder nameSb=new StringBuilder("select count(*) from bus_means_of_transaction where status =0 and name =?  ");
        ArrayList<Object> nameParams = CollectionUtil.newArrayList();
        nameParams.add(depositName);
        //如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            nameSb.append(" and id <> ?");
            nameParams.add(id);
        }
        //查询重复记录的数量
        int countByName = dbUtils.getIntValue(nameSb.toString(),nameParams.toArray());

        //如果存在重复的记录，抛出异常，直接返回前端
        if (countByName >= 1) {
            throw new BusinessException(0,"該交易方式已存在");
        }
    }

}
